package com.wrial.offer;
/*
 * @Author  Wrial
 * @Date Created in 16:08 2020/4/2
 * @Description 两个链表的第一个公共节点
 */

public class FaceTest_52 {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        ListNode pA = headA;
        ListNode pB = headB;
        // 就是再此搜索两轮，如果在第二轮/第一轮pA和pB同时等于0那就表明没有找到
        while (pA != pB) {
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }
        return pA;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
